﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sasc.PV.BLL;
using Sasc.PV.DAL;
using System.Collections;
using Sasc.PV.Common;

namespace Sasc.PV.Web.Admin.SystemManage
{
    public partial class TreeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBindToGridview("");

                //设定按钮权限            
                AddImageButton.Visible = PublicMethod.StrIFIn("|099A|", PublicMethod.GetSessionValue("QuanXian"));
                ModifyImageButton.Visible = PublicMethod.StrIFIn("|099M|", PublicMethod.GetSessionValue("QuanXian"));
                DeleteImageButton.Visible = PublicMethod.StrIFIn("|099D|", PublicMethod.GetSessionValue("QuanXian"));
                ReportImageButton.Visible = PublicMethod.StrIFIn("|099E|", PublicMethod.GetSessionValue("QuanXian"));
            }
           
        }

        //查询
        protected void SearchImageButton_Click(object sender, ImageClickEventArgs e)
        {
            DataBindToGridview("");
        }
        //结果中查询
        protected void SearchImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //保存上一次查询结果
            string JJ = "0";
            for (int i = 0; i < this.GVData.Rows.Count; i++)
            {
                Label LabV = (Label)GVData.Rows[i].FindControl("LabVisible");
                JJ = JJ + "," + LabV.Text.Trim();
            }
            DataBindToGridview(JJ);
        }
        //添加
        protected void AddImageButton_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("TreeListAdd.aspx");
        }
        //修改
        protected void ModifyImageButton_Click(object sender, ImageClickEventArgs e)
        {
            string CheckStr = PublicMethod.CheckCbx(this.GVData, "CheckSelect", "LabVisible");
            string[] CheckStrArray = CheckStr.Split(',');
            Response.Redirect("TreeListModify.aspx?ID=" + CheckStrArray[0].ToString());
        }
        //删除
        protected void DeleteImageButton_Click(object sender, ImageClickEventArgs e)
        {
            string IDlist = PublicMethod.CheckCbx(this.GVData, "CheckSelect", "LabVisible");
            if (DbHelperSQL.ExecuteSQL("delete from ERPTreeList where ID in (" + IDlist + ")") == -1)
            {
                Response.Write("<script>alert('删除选中记录时发生错误！请重新登陆后重试！');</script>");
            }
            else
            {
                DataBindToGridview("");
                //写系统日志
                ERPRiZhi MyRiZhi = new ERPRiZhi();
                MyRiZhi.UserName =PublicMethod.GetSessionValue("UserName");
                MyRiZhi.DoSomething = "用户删除菜单管理信息";
                MyRiZhi.IpStr = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
                MyRiZhi.Add();
            }

        }
        //导出
        protected void ReportImageButton_Click(object sender, ImageClickEventArgs e)
        {
            string IDList = "0";
            for (int i = 0; i < GVData.Rows.Count; i++)
            {
                Label LabVis = (Label)GVData.Rows[i].FindControl("LabVisible");
                IDList = IDList + "," + LabVis.Text.ToString();
            }
            Hashtable MyTable = new Hashtable();
            MyTable.Add("TextStr", "显示文字");
            MyTable.Add("ImageUrlStr", "所用图片");
            MyTable.Add("ValueStr", "后台数值");
            MyTable.Add("NavigateUrlStr", "链接地址");
            MyTable.Add("Target", "目标框架");
            MyTable.Add("ParentID", "父节点");
            MyTable.Add("QuanXianList", "权限");
            MyTable.Add("PaiXuStr", "排序");
           DataToExcel.GridViewToExcel(DbHelperSQL.GetDataSet("select  TextStr,ImageUrlStr,ValueStr,NavigateUrlStr,Target,ParentID,QuanXianList,PaiXuStr  from ERPTreeList where ID in (" + IDList + ") order by ID desc"), MyTable, "Excel报表");
        }

        #region  分页方法
        protected void ButtonGo_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (GoPage.Text.Trim().ToString() == "")
                {
                    Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
                }
                else if (GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(GoPage.Text.Trim().ToString()) > GVData.PageCount)
                {
                    Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
                }
                else if (GoPage.Text.Trim() != "")
                {
                    int PageI = Int32.Parse(GoPage.Text.Trim()) - 1;
                    if (PageI >= 0 && PageI < (GVData.PageCount))
                    {
                        GVData.PageIndex = PageI;
                    }
                }

                if (TxtPageSize.Text.Trim().ToString() == "")
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不可以为空!');</script>");
                }
                else if (TxtPageSize.Text.Trim().ToString() == "0")
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                }
                else if (TxtPageSize.Text.Trim() != "")
                {
                    try
                    {
                        int MyPageSize = int.Parse(TxtPageSize.Text.ToString().Trim());
                        this.GVData.PageSize = MyPageSize;
                    }
                    catch
                    {
                        Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                    }
                }

                DataBindToGridview("");
            }
            catch
            {
                DataBindToGridview("");
                Response.Write("<script language='javascript'>alert('请输入有效数字！');</script>");
            }
        }

        protected void PagerButtonClick(object sender, ImageClickEventArgs e)
        {
            //获得Button的参数值
            string arg = ((ImageButton)sender).CommandName.ToString();
            switch (arg)
            {
                case ("Next"):
                    if (this.GVData.PageIndex < (GVData.PageCount - 1))
                        GVData.PageIndex++;
                    break;
                case ("Pre"):
                    if (GVData.PageIndex > 0)
                        GVData.PageIndex--;
                    break;
                case ("Last"):
                    try
                    {
                        GVData.PageIndex = (GVData.PageCount - 1);
                    }
                    catch
                    {
                        GVData.PageIndex = 0;
                    }

                    break;
                default:
                    //本页值
                    GVData.PageIndex = 0;
                    break;
            }
            DataBindToGridview("");
        }

  
        #endregion

        protected void GVData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            PublicMethod.GridViewRowDataBound(e);

        
        }


        public void DataBindToGridview(string IDList)
        {
            ERPTreeList MyModel = new ERPTreeList();
            if (IDList.Trim().Length > 0)
            {
                GVData.DataSource = MyModel.GetList(" " + DropDownList2.SelectedItem.Value.ToString() + " like '%" + this.TextBox3.Text.Trim() + "%' and ID in(" + IDList + ") order by ParentID asc,PaiXuStr asc,ID asc");
            }
            else
            {
                GVData.DataSource = MyModel.GetList(" " + DropDownList2.SelectedItem.Value.ToString() + " like '%" + this.TextBox3.Text.Trim() + "%' order by ParentID asc,PaiXuStr asc,ID asc");
            }
            GVData.DataBind();
            LabPageSum.Text = Convert.ToString(GVData.PageCount);
            LabCurrentPage.Text = Convert.ToString(((int)GVData.PageIndex + 1));
            this.GoPage.Text = LabCurrentPage.Text.ToString();
            
        }
    }
}